<template>
  <el-dialog
    title="线索分配"
    v-model="dialogVisible"
    width="600px"
    @close="handleClose"
  >
    <el-form :model="formData" label-width="100px">
      <!-- 部门多选 -->
      <el-form-item label="选择部门">
        <el-select
          v-model="formData.deptIds"
          multiple
          filterable
          placeholder="请选择部门"
          style="width: 100%"
        >
          <el-option
            v-for="item in deptOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <!-- 员工多选 -->
      <el-form-item label="选择员工">
        <el-select
          v-model="formData.userIds"
          multiple
          filterable
          placeholder="请选择员工"
          style="width: 100%"
        >
          <el-option
            v-for="item in userOptions"
            :key="item.id"
            :label="item.nickname"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <!-- API客户 -->
      <el-form-item label="API客户">
        <el-input 
          v-model="formData.apiCode"
          placeholder="请输入API客户编码"
        />
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="loading">
        确定
      </el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
import * as DeptApi from '@/api/system/dept'
import * as UserApi from '@/api/system/user'
import { LeadsApi } from '@/api/weiyi/leads'
import type { DeptVO } from '@/api/system/dept'
import type { UserVO } from '@/api/system/user'

const props = defineProps({
  modelValue: {
    type: Boolean,
    required: true
  },
  selectedIds: {
    type: Array as PropType<number[]>,
    required: true
  }
})

const emit = defineEmits(['update:modelValue', 'success'])

// 弹窗显示状态
const dialogVisible = ref(false)
watch(() => props.modelValue, val => {
  dialogVisible.value = val
})
watch(dialogVisible, val => {
  emit('update:modelValue', val)
})

// 表单数据
const formData = ref({
  deptIds: [] as number[],
  userIds: [] as number[],
  apiCode: ''
})

// 部门选项
const deptOptions = ref<DeptVO[]>([])
// 员工选项
const userOptions = ref<UserVO[]>([])
// 加载状态
const loading = ref(false)

// 初始化数据
const initData = async () => {
  try {
    // 加载部门列表
    const deptRes = await DeptApi.getDeptList({
      status: 0 // 只查询正常状态的部门
    })
    deptOptions.value = deptRes
    
    // 加载员工列表
    const userRes = await UserApi.getUserPage({ 
      pageNo: 1, 
      pageSize: 100,
      status: 0 // 只查询正常状态的用户
    })
    userOptions.value = userRes.list
  } catch (error) {
    console.error('加载选项数据失败:', error)
    ElMessage.error('加载选项数据失败')
  }
}

// 提交分配
const handleSubmit = async () => {
  loading.value = true
  try {
    const { deptIds, userIds, apiCode } = formData.value
    
    // 分配到部门
    if (deptIds.length > 0) {
      await LeadsApi.assignToDept({
        dataIds: props.selectedIds,
        deptIds
      })
    }
    
    // 分配到员工
    if (userIds.length > 0) {
      await LeadsApi.assignToUser({
        dataIds: props.selectedIds,
        userIds
      })
    }
    
    // 分配到API客户
    if (apiCode) {
      await LeadsApi.assignToApi({
        dataIds: props.selectedIds,
        apiCode
      })
    }
    
    ElMessage.success('分配成功')
    dialogVisible.value = false
    emit('success')
  } catch (error) {
    console.error('分配失败:', error)
    ElMessage.error('分配失败')
  } finally {
    loading.value = false
  }
}

// 关闭弹窗
const handleClose = () => {
  formData.value = {
    deptIds: [],
    userIds: [],
    apiCode: ''
  }
}

// 初始化
initData()
</script>
